using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xfm.Core.Options;
using Xfm.TagHelpers;
using Xfm.TestEntities;

namespace Xfm.Simple
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool<EfTestContext>(options =>
            {
                options.UseNpgsql(Configuration.GetConnectionString("pgsql"),u=> {
                    u.SetPostgresVersion(13, 1);
                });
                //options.UseMySql("server=localhost;database=sakila;user=root;password=Abc.123456789;treattinyasboolean=true", x => x.ServerVersion("8.0.19-mysql"));//.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
            }, 100);

            services.AddControllersWithViews(options =>
            {
            })
                .AddRazorRuntimeCompilation()
                ;
            XfmConfig xfConfig = new XfmConfig();
            IXfmOption nav = new XfJsonOption(typeof(NavModel), "nav_test");
            xfConfig.AddOption(nav);
            xfConfig.AddNavMenu("nav_admin");
            services.AddXfmFramework(xfConfig);
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
            {
                options.LoginPath = "/Account/Login";
                options.LogoutPath = "/Account/Logout";
                options.AccessDeniedPath = "/Account/Denied";
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();


            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
